package com.deliinsns.app.company;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.deliinsns.app.company.bean.Company;
import com.deliinsns.sys.BasicController;
import com.deliinsns.sys.MsgType;
import com.deliinsns.sys.util.SystemConstant;
import com.deliinsns.sys.util.ParameterUtil;
@Controller
public class CompanyController extends BasicController {
	
	 @Autowired  
     private  CompanyDao companyDao;  
	
	@RequestMapping(value = "/company/get/{companyId}", method = RequestMethod.GET)
	 public void getCompany(@PathVariable String companyId ,HttpServletRequest request,HttpServletResponse response)throws Exception {
		if(checkParam(companyId)){				//如果所有参数都正确传入才进行相应处理
			Company company= companyDao.load(Company.class, Integer.parseInt(companyId));
			  if(company!=null){
				  msg.setBean(company);
				  msg.setState(200);
				  msg.setMsg(SystemConstant.GET_SUCCESS);
			  }else{
				  msg.setState(202);
				  msg.setMsg(SystemConstant.GET_FAILD);
			  }
		}else{
			msg.setMsg(SystemConstant.PARAM_ERROR);
		}
		out(request,response,MsgType.msg);;
	 }
	
	@RequestMapping(value = "/company/list", method = RequestMethod.GET)
	 public void listCompany(HttpServletRequest request,HttpServletResponse response)throws Exception {
		String page= request.getParameter("page");//第几页
		String size= request.getParameter("size");//页的大小
		if(checkParam(page,size)&&ParameterUtil.allNumeric(page,size)){				//如果所有参数都正确传入才进行相应处理
			
			Integer pageNo= Integer.parseInt(page);
			Integer pageSize= Integer.parseInt(size);
			List<Company> list= null;
			DetachedCriteria criteria = DetachedCriteria.forClass(Company.class);
		    criteria.add(Property.forName("name").eq("name")) ;
			list= companyDao.findByCriteria(criteria,pageNo*pageSize,pageSize);
			msg.setItems(list);
			msg.setTotal(100);//此处的total有待查询？？？？？？？
		}else{
			msg.setMsg(SystemConstant.PARAM_ERROR);
		}
		out(request,response,MsgType.msg);;
	 }	
	
	@RequestMapping(value = "/company/save", method = RequestMethod.POST)
	 public void saveCompany(Company company,HttpServletRequest request,HttpServletResponse response)throws Exception {
		String id =(company!=null)?company.getId():null;
		try{ 
			companyDao.saveOrUpdate(company);
		} 
		catch(Exception e) 
		{ 
			msg.setMsg(e.getMessage());
			out(request,response,MsgType.msg);;
		}
		msg.setState(200);
		if(StringUtils.isNotEmpty(id))
			msg.setMsg(SystemConstant.UPDATE_SUCCESS);
		else
			msg.setMsg(SystemConstant.ADD_SUCCESS);
		out(request,response,MsgType.msg);
	 }
	
	@RequestMapping(value = "/company/delete/{companyIds}", method = RequestMethod.POST)
	 public void deleteCompany(@PathVariable String companyIds ,HttpServletRequest request,HttpServletResponse response)throws Exception {
		if(checkParam(companyIds)){				//如果所有参数都正确传入才进行相应处理
			try{
				String hql = "from Company m where m.id in"+"("+companyIds+")";
				List<Company> companys= companyDao.findByHql(hql);
				companyDao.delete(companys);
				msg.setState(200);
				msg.setMsg(SystemConstant.DELETE_SUCCESS);
			}catch(Exception e){
				msg.setMsg(e.getMessage());
				out(request,response,MsgType.msg);;
			}
		}else{
			msg.setMsg(SystemConstant.PARAM_ERROR);
		}
		out(request,response,MsgType.msg);;
	 }
	

}
